package com.wcl.demo.ui.activity.splash;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wcl.demo.R;
import com.wcl.demo.ui.activity.BaseActivity;
import com.wcl.demo.ui.activity.LoginPageActivity;
import com.wcl.demo.ui.adapter.SplashViewPagerAdapter;
import com.wcl.demo.utils.ActivityUtils;
import com.wcl.demo.view.CustomGridLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by dllo on 15/11/20.
 */
public class SplashActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private List<View> views;
    private SplashViewPagerAdapter adapter;
    private  Button splash_into;

    // 底部小圆点
    private ImageView[] dots;
    // 记录当前选中位置
    private int currentIndex;

    private int[] imageTwo = {R.mipmap.splash2_1, R.mipmap.shlash2_2, R.mipmap.shlasp2_3, R.mipmap.shlasp2_4};
    private int[] imageThr = {R.mipmap.splash3_1, R.mipmap.splash3_2, R.mipmap.splash3_3, R.mipmap.splash3_4
            , R.mipmap.splash3_5, R.mipmap.splash3_6, R.mipmap.splash3_7, R.mipmap.splash3_8, R.mipmap.splash3_9};

    @Bind(R.id.ll)
    LinearLayout ll;
    @Bind(R.id.splash_viewPager)
    ViewPager splash_viewPager;

    @Override
    protected int getLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        adapter = new SplashViewPagerAdapter();

        LayoutInflater inflater = LayoutInflater.from(this);
        View view1 = inflater.inflate(R.layout.activity_splash_one, null);
        splash_into= (Button) view1.findViewById(R.id.splash_into);
        View view2 = inflater.inflate(R.layout.activity_splash_two, null);
        CustomGridLayout splash_two = (CustomGridLayout) view2.findViewById(R.id.splash_two);
        View view3 = inflater.inflate(R.layout.avtivity_splash_thr, null);
        CustomGridLayout splash_thr = (CustomGridLayout) view3.findViewById(R.id.splash_thr);

        initMyGrid(splash_two, inflater, imageTwo);
        initMyGrid(splash_thr, inflater, imageThr);
        views = new ArrayList<View>();
        views.add(view3);
        views.add(view2);
        views.add(view1);

    }

    private void initMyGrid(CustomGridLayout customGridLayout, final LayoutInflater inflater, final int[] images) {
        customGridLayout.setGridAdapter(new CustomGridLayout.GridAdatper() {
            @Override
            public View getView(int index) {
                View view = inflater.inflate(R.layout.activity_splash_page_item, null);
                final ImageView image_item = (ImageView) view.findViewById(R.id.splash_image_item);
                image_item.setImageResource(images[index]);
                return view;
            }

            @Override
            public int getCount() {
                return images.length;
            }
        });
    }

    @Override
    protected void initData() {
        adapter.addData(views);
        splash_viewPager.setAdapter(adapter);
      //  splash_viewPager.setPageTransformer(true, new CubeTransformer());
        splash_viewPager.setOnPageChangeListener(this);
        initDots();
        splash_into.setOnClickListener(this);
    }

    private void initDots() {
        dots = new ImageView[views.size()];

        // 循环取得小点图片
        for (int i = 0; i < views.size(); i++) {
            dots[i] = (ImageView) ll.getChildAt(i);
            dots[i].setEnabled(true);// 都设为灰色
        }

        currentIndex = 0;
        dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > views.size() - 1
                || currentIndex == position) {
            return;
        }

        dots[position].setEnabled(false);
        dots[currentIndex].setEnabled(true);

        currentIndex = position;
    }

    // 当滑动状态改变时调用

    public void onPageScrollStateChanged(int arg0) {
    }

    // 当当前页面被滑动时调用

    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    // 当新的页面被选中时调用

    public void onPageSelected(int arg0) {
        // 设置底部小点选中状态
        setCurrentDot(arg0);
    }

    @Override
    public void onClick(View v) {
        ActivityUtils.startToActivity(this, LoginPageActivity.class);
    }
}
